﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RomanJendrusz.Amazon.ETL.Infrastructure.IoC;
using System.Data.Linq;
using RomanJendrusz.Amazon.ETL.Repositories;

namespace RomanJendrusz.Amazon.ETL.Domain
{
    public class ETLConfiguration
    {
        private DateTime extractStartDate;
        private DateTime extractEndDate;
        private bool wasNotLoaded;
        private bool hasCompletedSuccessful;
        private int id;

        public ETLConfiguration()
        {
            id = 0;
            extractStartDate = DateTime.Now;
            extractEndDate = extractStartDate;
            wasNotLoaded = false;
            hasCompletedSuccessful = false;
        }

        public DateTime ExtractStartDate
        {
            get { return extractStartDate; }
        }

        public DateTime ExtractEndDate
        {
            get { return extractEndDate; }
        }

        public static void BeginETL()
        {
            var factory = IoCGateway.Resolve<IUnitOfWorkFactory>();
            using (var work = factory.BeginUnitOfWork())
            {
                var config = work.Configuration;
                if (config.wasNotLoaded == false)
                {
                    config.extractStartDate = DateTime.Now;
                }
                config.hasCompletedSuccessful = false;
                config.wasNotLoaded = true;
            }
        }

        public static void EndETL()
        {
            var factory = IoCGateway.Resolve<IUnitOfWorkFactory>();
            using (var work = factory.BeginUnitOfWork())
            {
                var config = work.Configuration;
                config.extractEndDate = DateTime.Now;
                config.hasCompletedSuccessful = true;
                work.SubmitChanges();
            }
        }
        
    }
}
